package com.woniuxy.oldmanapi.service;

import cn.hutool.core.date.chinese.ChineseMonth;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.CheckIn;
import com.woniuxy.oldmanapi.mapper.CardMapper;
import com.woniuxy.oldmanapi.mapper.CheckInMapper;
import com.woniuxy.oldmanapi.mapper.OlderMapper;
import com.woniuxy.oldmanapi.xvo.OlderVo;
import com.woniuxy.oldmanapi.zvo.CheckInVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CheckInService {
    @Autowired
    CheckInMapper checkInMapper;
    @Autowired
    OlderMapper olderMapper;
    @Autowired
    CardMapper cardMapper;

    public CheckInVo selectCheckInById(int id) {
        return checkInMapper.selectCheckInById(id);
    }

    public PageInfo<CheckInVo> selectCheckIn(int now, int size, String name, Date beginDate,Date endDate,int status) {
        PageHelper.startPage(now,size);
        return new PageInfo<>(checkInMapper.selectCons(name,beginDate,endDate,status));
    }

    public void cancelCheckIn(int id) {
        checkInMapper.cancelCheckIn(id);
    }
    @Transactional
    public void insertCheckIn(CheckInVo consulter) throws ParseException {
        CheckIn checkIn=new CheckIn();
        Card card = cardMapper.selectByPrimaryKey(consulter.getCardId());
        checkIn.setICCard(card.getNumber());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        if (consulter.getTime()!=null && consulter.getTime().length!=0) {
            consulter.setBegindate(dateFormat.parse(consulter.getTime()[0]));
            consulter.setEnddate(dateFormat.parse(consulter.getTime()[1]));
        }
        checkIn.setBegindate(consulter.getBegindate());
        checkIn.setDescription(consulter.getDescription());
        checkIn.setEnddate(consulter.getEnddate());
        checkIn.setStatus(consulter.getStatus());
        checkIn.setOlderId(consulter.getOlderId());
        checkIn.setTotalprice(consulter.getTotalprice());
        checkIn.setStatus(1);
        BigDecimal bigDecimal=new BigDecimal(consulter.getTotalprice());

        if(card.getStatus()!=1){
            throw  new RuntimeException("此卡已被冻结或挂失，非正常状态，不可用！");
        }
        if(card.getBalance().compareTo(bigDecimal)<0){
            throw  new RuntimeException("余额不足");
        }
        cardMapper.updateMoneyByNumAndpay(card.getNumber(),bigDecimal);
        //将老人状态改为出院
        //olderMapper.updateOlderStatusById(consulter.getOlderId());
        checkInMapper.insertTo(checkIn);
    }

    public OlderVo selectRoomPayById(int id) {
        return checkInMapper.selectRoomPayById(id);
    }

    public OlderVo selectCarePayById(int id) {
        return checkInMapper.selectCarePayById(id);
    }
}
